package org.eiahe.hr.performance.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.eiahe.hr.performance.domain.PerformanceIndicator;
import org.eiahe.hr.performance.domain.bo.PerformanceIndicatorBo;
import org.eiahe.hr.performance.service.IPerformanceIndicatorService;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import java.util.List;

/**
 * 绩效指标Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/performance/indicator")
public class PerformanceIndicatorController extends BaseController {

    private final IPerformanceIndicatorService performanceIndicatorService;

    /**
     * 查询绩效指标列表
     */
    @SaCheckPermission("performance:indicator:list")
    @GetMapping("/list")
    public TableDataInfo<PerformanceIndicator> list(PerformanceIndicatorBo indicator, PageQuery pageQuery) {
        indicator.setParentId(0L);
        return performanceIndicatorService.queryPageList(indicator, pageQuery);
    }

    /**
     * 查询子指标列表
     */
    @SaCheckPermission("performance:indicator:list")
    @GetMapping("/children/{parentId}")
    public R<List<PerformanceIndicator>> listChildren(@PathVariable("parentId") Long parentId) {
        List<PerformanceIndicator> children = performanceIndicatorService
                .list(new LambdaQueryWrapper<PerformanceIndicator>()
                        .eq(PerformanceIndicator::getParentId, parentId)
                        .eq(PerformanceIndicator::getDelFlag, "0")
                        .orderByAsc(PerformanceIndicator::getSortOrder));
        return R.ok(children);
    }

    /**
     * 获取绩效指标详细信息
     */
    @SaCheckPermission("performance:indicator:query")
    @GetMapping(value = "/{indicatorId}")
    public R<PerformanceIndicator> getInfo(@PathVariable("indicatorId") Long indicatorId) {
        return R.ok(performanceIndicatorService.queryById(indicatorId));
    }

    /**
     * 新增绩效指标
     */
    @SaCheckPermission("performance:indicator:add")
    @Log(title = "绩效指标", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody PerformanceIndicator indicator) {
        return toAjax(performanceIndicatorService.insertByBo(indicator));
    }

    /**
     * 修改绩效指标
     */
    @SaCheckPermission("performance:indicator:edit")
    @Log(title = "绩效指标", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody PerformanceIndicator indicator) {
        return toAjax(performanceIndicatorService.updateByBo(indicator));
    }

    /**
     * 删除绩效指标
     */
    @SaCheckPermission("performance:indicator:remove")
    @Log(title = "绩效指标", businessType = BusinessType.DELETE)
    @DeleteMapping("/{indicatorIds}")
    public R<Void> remove(@PathVariable Long[] indicatorIds) {
        return toAjax(performanceIndicatorService.deleteWithValidByIds(List.of(indicatorIds), true));
    }
}
